﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.CoffeShop.Model;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using Truextend.CoffeShop.Bussiness;
using Truextend.CoffeShop.Service;

namespace Truextend.CoffeShop.Presenter.Orders
{
    public class RegisterOrderPresenter
    {
        IRegisterOrder view;        

        public RegisterOrderPresenter(IRegisterOrder view)
        {
            this.view = view;
        }                

        public void FillRestaurants()
        {
            RestaurantService service = new RestaurantService();
            IList<Restaurant> listRestaurants = service.GetExternalRestaurant();
            Hashtable hash = new Hashtable();

            hash.Add("0", "Choose an option");

            foreach(var restaurants in listRestaurants){
                hash.Add(restaurants.RestaurantId, restaurants.Name);
            }            

            view.listRestaurants = hash;            

        }

        public void FillDatesTimes()
        {
            view.startRequest = DateTime.Now;
            view.endRequestDate = DateTime.Now;
            view.expectedDeliveryDate = DateTime.Now;
        }

        public bool InsertOrder()
        {
            if(IsDataValid())
            {
                OrderService service = new OrderService();

                Order order = new Order() { 
                    UserID = view.userId,
                    UserName = view.userName,
                    StartDate = view.startRequest,
                    EndDate = view.endRequestDate,
                    ExpectedDeliveryDate = view.expectedDeliveryDate,
                    Drink = view.drink,
                    Title = view.title,
                    State = "CRE",                
                };

                List<int> listProductsId = FillSelectedProducts();

                service.AddOrder(order, listProductsId);
                
                return true;
            }

            return false;
        }

        private bool IsDataValid()
        {
            bool isValid = true;

            // Location Error
            if (view.title.Trim().Equals(string.Empty))
            {
                view.SetError("Title is a required field", ViewPanelError.Title);
                isValid = false;
            }

            // Restaurant and Options Errors
            if (view.restaurant.Trim().Equals("Choose an option"))
            {
                view.SetError("Choose an option please", ViewPanelError.Restaurant);
                isValid = false;
            }
            else
            {
                if(view.options.Rows.Count == 0 )
                {
                    view.SetError("Choose a one product please", ViewPanelError.Options);
                    isValid = false;
                }
            }
            
            // Dates Errors
            if ( 0 >=  view.endRequestDate.Subtract(view.startRequest).Minutes)
            {
                view.SetError("End Request Date must be greater than the Start Request Date", ViewPanelError.Dates);
                isValid = false;
            }                        
            
            // DeleveryTime Error
            if (0 >= view.endRequestDate.Subtract(view.startRequest).Minutes)
            {
                view.SetError("Expected Delivery Date must be greater than the End Request Date", ViewPanelError.DeleveryTime);
                isValid = false;
            }                        

            return isValid;
        }

        private List<int> FillSelectedProducts()
        {
            List<int> listProductsId = new List<int>();

            foreach (DataRow row in view.options.Rows) {

                listProductsId.Add(int.Parse(row["OptionID"].ToString()));
            
            }

            return listProductsId;

        }

        public void FillOptions(string restaurantId)
        {
            ProductService service = new ProductService();

            IList<Product> listProdructs = service.GetAllProductsForRestaurant(int.Parse(restaurantId));

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("OptionID");
            dataTable.Columns.Add("Description");
            dataTable.Columns.Add("Price");
            dataTable.Columns.Add("CheckBoxOption",typeof(bool));

            foreach(var products in listProdructs){

                DataRow row = dataTable.NewRow();
                row["OptionID"] = products.ProductId;
                row["Description"] = products.Name;
                row["Price"] = products.Price;
                row["CheckBoxOption"] = false;
                dataTable.Rows.Add(row);

            }

            view.options = dataTable;            
        }
        
    }

    public enum ViewPanelError
    {
        Title, Restaurant, Options, Dates, DeleveryTime
    }
}
